package com.citycloud.ccuap.tc.admin.engine;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.citycloud.ccuap.commons.application.Application;
import com.citycloud.ccuap.commons.engine.base.AbstractEngine;
import com.citycloud.ccuap.commons.engine.base.IEngine;
import com.citycloud.ccuap.tc.admin.sec.secuser.SecUserCacheService;
import com.citycloud.ccuap.tc.yq.meatbustype.IMeatBusTypeService;
import com.citycloud.ccuap.tc.yq.resultsymbol.IResultSymbolService;

/**
 * 业务类型和符号表加载
 * 
 * @author zhusr
 * 
 */
public class YqEngine extends AbstractEngine implements IEngine {
	private static YqEngine instance;

	private YqEngine(String profile) {
	}

	public static YqEngine getInstance(String profile) {
		if (instance == null) {
			instance = new YqEngine(profile);
		}
		return instance;
	}

	@Override
	public void initialize(String profile) {
		try {
			IMeatBusTypeService busTypeService = Application.getClassBean(IMeatBusTypeService.class);
			busTypeService.loadDataToMemory();
			
			IResultSymbolService resultSymbolService = Application.getClassBean(IResultSymbolService.class);
			resultSymbolService.loadDataToMemory();
			
			logger.info("yq-api initdata ...");
			RedisTemplate redisTemplate = Application.getClassByName("redisTemplate");
			redisTemplate.setKeySerializer(new StringRedisSerializer());
			redisTemplate.setHashKeySerializer(new StringRedisSerializer());
			redisTemplate.setDefaultSerializer(new StringRedisSerializer());
			//redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
			//redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
			//获取开始时间
			long startTime = System.currentTimeMillis();
			logger.info("----------------------开始构建缓存----------------------");
			SecUserCacheService secUserCacheService = Application.getClassBean(SecUserCacheService.class);
			secUserCacheService.buildAllDataGroupAndRoleCache();
			//获取结束时间
			long endTime = System.currentTimeMillis();
			//平台启动后，如果要实现初始化操作可以在此实现
			logger.info("----------------------构建缓存结束----------------------");
			logger.info("----------------------构建时间：" + (endTime - startTime) / 1000 + "s----------------------");
			logger.info("yq-api initdata complete.");
		} catch (Exception ex) {
			logger.error("=============## YqEngine 加载失败！=============");
			ex.printStackTrace();
		}

		logger.info("=============## YqEngine 加载成功##================");
	}
	
	@Override
	public void destroy(String profile) {
		logger.info("=============## YqEngine 销毁##================");
	}
}
